package com.widevision.ListActivity;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import net.widevision.android.R;
import net.widevision.android.TwitterApp;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.User;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.widevision.adapter.FrndListAdapter;

public class FrndName extends Activity{
	
	public static final String FRND_ID = "f_id";
	public static final String FRND_NAME = "frnd_name";
	public static final String FRND_THUMB_URL = "friend_thumb_url";
	private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
	private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	
	ArrayList<HashMap<String, String>> frndList = new ArrayList<HashMap<String, String>>();
	
    FrndListAdapter frndListAdapter;
	
    ResponseList<twitter4j.User> frndlisttext;
    
    User user,user1;
    
    TwitterApp mTwitter;
    
    InputStream is;
    
    Drawable frndsimg;
    
    ListView frndlistview;
    
    TextView Scroll;
    
    String frndname,screeName;
    
    String userId,userid;
    
    int Id;
    
    int id;
    
    int count;
    
    int Userid;
    
    Twitter tTwitter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frndlist);
		frndlistview    =(ListView) findViewById(R.id.frndidlist);
		Scroll          =(TextView) findViewById(R.id.scroller1);
		Bundle bundle=getIntent().getExtras();
	    Userid=bundle.getInt("USERID");

		try
		{
			prepareArralist();
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();
		}
		
		frndListAdapter  = new FrndListAdapter(FrndName.this, frndList);
		frndlistview.setAdapter(frndListAdapter);

		Scroll.setSelected(true);
		
		Scroll.setText("@"+screeName+" "+"is Following"+"                                                      ");
		
		frndlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
				
				TextView txt = (TextView) view.findViewById(R.id.userId);
				
				userid=txt.getText().toString();
			 
			    Intent intent=new Intent(FrndName.this, UserProfile.class);
			    
			    id=Integer.parseInt(userid);
			    
			    intent.putExtra("USERID", id);
			    
			    startActivity(intent);
			}
		});
	}

	private void prepareArralist()
	{
			    
		    mTwitter 	= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
		    
			  if (mTwitter.hasAccessToken())
			 {
				 
	         user=mTwitter.showUser(Userid);
	        
	         screeName=user.getScreenName();

  		     count=user.getFriendsCount();
		
			 frndlisttext=mTwitter.Friendsids(Userid);
			 try {
						for(int i=0;i<count;i++)
						{
							
							HashMap<String, String> map = new HashMap<String, String>();
							
							user1=frndlisttext.get(i);
						
							Id=user1.getId();
						
							userId=String.valueOf(Id);
						
							map.put(FRND_ID,userId);
						
							frndname=user1.getScreenName();
						
							map.put(FRND_NAME,frndname);
						
							URL u=user1.getProfileImageURL();
						
//						try
//						{
//								is = u.openStream();
//						} 
//						catch (IOException e)
//						{
//								e.printStackTrace();
//						}
//						frndsimg = Drawable.createFromStream(is, "src name");
						map.put(FRND_THUMB_URL,""+u);
						frndList.add(map);
		
					
						}
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
	    }
			
			else
			{
				Log.d("ELSE PART ", " U R NOT LOGGED IN YET...");
			}
    }
    
    
    //Hello i am mahendra team member have you seen my changessss

	
   }
